package cn.hznc.controller.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.domain.entity.*;
import cn.hznc.domain.request.order.AbutmentOrderReq;
import cn.hznc.mapper.*;
import cn.hznc.repository.AbutmentEntryExitRecordRepository;
import cn.hznc.repository.AbutmentProcessCraftRepository;
import cn.hznc.repository.AbutmentSubProgramRepository;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
//import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@DS("sqlserver")
public class QuerySqlServerService {
    @Resource
    private AbutmentProgramMapper abutmentProgramMapper;
    @Resource
    private AbutmentProductionMapper abutmentProductionMapper;
    @Resource
    private AbutmentSubProgramRepository abutmentSubProgramRepository;
    @Resource
    private AbutmentProcessCraftRepository abutmentProcessCraftRepository;
    @Resource
    private AbutmentOrderMapper abutmentOrderMapper;
    @Resource
    private AbutmentEntryExitRecordRepository abutmentEntryExitRecordRepository;
    @Resource
    private AbutmentProductionBomMapper abutmentProductionBomMapper;
    @Resource
    private AbutmentOrderCraftMapper abutmentOrderCraftMapper;

    public List<AbutmentProgramEntity> abutmentSubProgramReload(){
        LambdaQueryWrapper<AbutmentProgramEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<AbutmentProgramEntity> abutmentProgramEntityList = abutmentProgramMapper.selectList(lambdaQueryWrapper);
        abutmentProgramEntityList.forEach(abutmentProgramEntity -> {
            abutmentProgramEntity.setConnectSubProgram(queryAbutmentSubProgram(abutmentProgramEntity.getProgramCn()));
        });
        return abutmentProgramEntityList;
    }
    public List<AbutmentProgramEntity> abutmentSubProgramReload1(List<Map<String , String>> materialNoList){
        List<String> partnoList = new ArrayList<>();
        materialNoList.forEach(stringStringMap -> {
            partnoList.add(stringStringMap.get("materialNo"));
        });
        LambdaQueryWrapper<AbutmentProgramEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!CollectionUtils.isEmpty(materialNoList)){
            lambdaQueryWrapper.in(AbutmentProgramEntity::getMaterialNo,partnoList);
        }
        List<AbutmentProgramEntity> abutmentProgramEntityList = abutmentProgramMapper.selectList(lambdaQueryWrapper);
        abutmentProgramEntityList.forEach(abutmentProgramEntity -> {
            abutmentProgramEntity.setConnectSubProgram(queryAbutmentSubProgram(abutmentProgramEntity.getProgramCn()));
        });
        return abutmentProgramEntityList;
    }

    public List<AbutmentProgramEntity> abutmentSubProgramReload(List<String> materialNoList){
        LambdaQueryWrapper<AbutmentProgramEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!CollectionUtils.isEmpty(materialNoList)){
            lambdaQueryWrapper.in(AbutmentProgramEntity::getMaterialNo,materialNoList);
        }
        List<AbutmentProgramEntity> abutmentProgramEntityList = abutmentProgramMapper.selectList(lambdaQueryWrapper);
        abutmentProgramEntityList.forEach(abutmentProgramEntity -> {
            abutmentProgramEntity.setConnectSubProgram(queryAbutmentSubProgram(abutmentProgramEntity.getProgramCn()));
        });
        return abutmentProgramEntityList;
    }

    public List<AbutmentSubProgramEntity> queryAbutmentSubProgram(String programCn){
        List<AbutmentSubProgramEntity> abutmentSubProgramEntities = abutmentSubProgramRepository.queryAbutmentSubProgramData(programCn);

        return abutmentSubProgramEntities;

    }

    public List<AbutmentProductionEntity> abutmentProduction(){
        LambdaQueryWrapper<AbutmentProductionEntity> abutmentProductionEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        abutmentProductionEntityLambdaQueryWrapper.ne(AbutmentProductionEntity::getFigureNo,"");

        List<AbutmentProductionEntity> abutmentProductionEntityList = abutmentProductionMapper.selectList(abutmentProductionEntityLambdaQueryWrapper);
        abutmentProductionEntityList.forEach(abutmentProductionEntity -> {
            LambdaQueryWrapper<AbutmentOrderEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AbutmentOrderEntity::getMaterialNo,abutmentProductionEntity.getMaterialNo());
            lambdaQueryWrapper.eq(AbutmentOrderEntity::getProductionBranchFactory,12);
            lambdaQueryWrapper.ne(AbutmentOrderEntity::getAbutmentStatus,9);
            List<AbutmentOrderEntity> abutmentOrderEntityList = abutmentOrderMapper.selectList(lambdaQueryWrapper);
            if (CollectionUtil.isNotEmpty(abutmentOrderEntityList)) {
                List<AbutmentProcessCraftEntity> abutmentProcessCraftEntityList = queryAbutmentProcessCraft(abutmentOrderEntityList.get(0).getTaskId());
                abutmentProcessCraftEntityList.forEach(abutmentProcessCraftEntity -> {
                    try {
                        abutmentProcessCraftEntity.setInProcessTime((abutmentProcessCraftEntity.getStartTime().getTime() - abutmentProcessCraftEntity.getEndTime().getTime()) / (24 * 60 * 60 * 1000));
                    }catch (NullPointerException e){
//                        throw new NullPointerException("该工序缺少最早转入时间或最后转出时间");
                    }
                });
                abutmentProductionEntity.setConnectProcessCrafts(abutmentProcessCraftEntityList);
            }
        });
        return abutmentProductionEntityList;
    }
    public List<AbutmentProductionEntity> abutmentProduction(List<String> materialNoList){
        LambdaQueryWrapper<AbutmentProductionEntity> abutmentProductionEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        abutmentProductionEntityLambdaQueryWrapper.ne(AbutmentProductionEntity::getFigureNo,"");
        if (!CollectionUtils.isEmpty(materialNoList)){
            abutmentProductionEntityLambdaQueryWrapper.in(AbutmentProductionEntity::getMaterialNo,materialNoList);
        }
        List<AbutmentProductionEntity> abutmentProductionEntityList = abutmentProductionMapper.selectList(abutmentProductionEntityLambdaQueryWrapper);
        abutmentProductionEntityList.forEach(abutmentProductionEntity -> {
            LambdaQueryWrapper<AbutmentOrderEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AbutmentOrderEntity::getMaterialNo,abutmentProductionEntity.getMaterialNo());
            lambdaQueryWrapper.eq(AbutmentOrderEntity::getProductionBranchFactory,12);
            lambdaQueryWrapper.ne(AbutmentOrderEntity::getAbutmentStatus,9);
            List<AbutmentOrderEntity> abutmentOrderEntity = abutmentOrderMapper.selectList(lambdaQueryWrapper);
            if (CollectionUtil.isNotEmpty(abutmentOrderEntity)) {
                List<AbutmentProcessCraftEntity> abutmentProcessCraftEntityList = queryAbutmentProcessCraft(abutmentOrderEntity.get(0).getTaskId());
                abutmentProcessCraftEntityList.forEach(abutmentProcessCraftEntity -> {
                    try {
                        abutmentProcessCraftEntity.setInProcessTime((abutmentProcessCraftEntity.getStartTime().getTime() - abutmentProcessCraftEntity.getEndTime().getTime()) / (24 * 60 * 60 * 1000));
                    }catch (NullPointerException e){
//                        throw new NullPointerException("该工序缺少最早转入时间或最后转出时间");
                    }
                });
                abutmentProductionEntity.setConnectProcessCrafts(abutmentProcessCraftEntityList);
                LambdaQueryWrapper<AbutmentProductionBomEntity> lambdaQueryWrapper1 = new LambdaQueryWrapper();
                lambdaQueryWrapper1.eq(AbutmentProductionBomEntity::getTaskId,abutmentOrderEntity.get(0).getTaskId());
                List<AbutmentProductionBomEntity> abutmentProductionBomEntities = abutmentProductionBomMapper.selectList(lambdaQueryWrapper1);
                abutmentProductionEntity.setConnectProductionBom(abutmentProductionBomEntities);
            }
        });
        return abutmentProductionEntityList;
    }

    public List<AbutmentProductionEntity> abutmentProductionReload1(List<Map<String , String>> materialNoList){
        List<String> partnoList = new ArrayList<>();
//        List<String> taskIdList = new ArrayList<>();
        materialNoList.forEach(stringStringMap -> {
            partnoList.add(stringStringMap.get("materialNo"));
//            taskIdList.add(stringStringMap.get("taskId"));
        });
        LambdaQueryWrapper<AbutmentProductionEntity> abutmentProductionEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //abutmentProductionEntityLambdaQueryWrapper.ne(AbutmentProductionEntity::getFigureNo,"");
        if (!CollectionUtils.isEmpty(materialNoList)){
            abutmentProductionEntityLambdaQueryWrapper.in(AbutmentProductionEntity::getMaterialNo,partnoList);
        }
        List<AbutmentProductionEntity> abutmentProductionEntityList = abutmentProductionMapper.selectList(abutmentProductionEntityLambdaQueryWrapper);
        List<AbutmentProductionEntity> abutmentProductionEntityList1 = new ArrayList<>();
        abutmentProductionEntityList.forEach(abutmentProductionEntity -> {
            LambdaQueryWrapper<AbutmentOrderEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AbutmentOrderEntity::getMaterialNo,abutmentProductionEntity.getMaterialNo());
            lambdaQueryWrapper.eq(AbutmentOrderEntity::getProductionBranchFactory,12);
            lambdaQueryWrapper.ne(AbutmentOrderEntity::getAbutmentStatus,9);
            //lambdaQueryWrapper.groupBy(AbutmentOrderEntity::getMaterialNo);
            List<AbutmentOrderEntity> abutmentOrderEntity = abutmentOrderMapper.selectList(lambdaQueryWrapper);
            List<String> taskIds = new ArrayList<>();
            List<String> taskIds1 = new ArrayList<>();
            if (abutmentOrderEntity != null) {
                abutmentOrderEntity.forEach(abutmentOrderEntity1 -> {
                    taskIds.add(abutmentOrderEntity1.getTaskId());
                });
                materialNoList.forEach(stringStringMap -> {
                    if (taskIds.contains(stringStringMap.get("taskId"))){
                        taskIds1.add(stringStringMap.get("taskId"));
                    }
                });

//                List<AbutmentProcessCraftEntity> abutmentProcessCraftEntityList = queryAbutmentProcessCraft(abutmentOrderEntity.get(0).getTaskId());
//                abutmentProcessCraftEntityList.forEach(abutmentProcessCraftEntity -> {
//                    try {
//                        abutmentProcessCraftEntity.setInProcessTime((abutmentProcessCraftEntity.getStartTime().getTime() - abutmentProcessCraftEntity.getEndTime().getTime()) / (24 * 60 * 60 * 1000));
//                    }catch (NullPointerException e){
////                        throw new NullPointerException("该工序缺少最早转入时间或最后转出时间");
//                    }
//                });
//                abutmentProductionEntity.setConnectProcessCrafts(abutmentProcessCraftEntityList);

                taskIds1.forEach(taskId ->{
                    AbutmentProductionEntity abutmentProductionEntity1 = new AbutmentProductionEntity();
                    BeanUtils.copyProperties(abutmentProductionEntity , abutmentProductionEntity1);
                    List<AbutmentProcessCraftEntity> abutmentProcessCraftEntityList = queryAbutmentProcessCraft(taskId);
                    abutmentProductionEntity1.setConnectProcessCrafts(abutmentProcessCraftEntityList);
                    LambdaQueryWrapper<AbutmentProductionBomEntity> lambdaQueryWrapper1 = new LambdaQueryWrapper();
                    lambdaQueryWrapper1.eq(AbutmentProductionBomEntity::getTaskId,taskId);
                    List<AbutmentProductionBomEntity> abutmentProductionBomEntities = abutmentProductionBomMapper.selectList(lambdaQueryWrapper1);
                    abutmentProductionEntity1.setConnectProductionBom(abutmentProductionBomEntities);
                    abutmentProductionEntityList1.add(abutmentProductionEntity1);
                });

            }
        });
        return abutmentProductionEntityList1.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(AbutmentProductionEntity::getMaterialNo))), ArrayList::new)
        );
    }
    public List<AbutmentProcessCraftEntity> abutmentProcess(String materialNo){
        LambdaQueryWrapper<AbutmentOrderEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AbutmentOrderEntity::getMaterialNo,materialNo);
        lambdaQueryWrapper.eq(AbutmentOrderEntity::getProductionBranchFactory,12);
        lambdaQueryWrapper.ne(AbutmentOrderEntity::getAbutmentStatus,9);
        AbutmentOrderEntity abutmentOrderEntity = abutmentOrderMapper.selectOne(lambdaQueryWrapper);
        List<AbutmentProcessCraftEntity> abutmentProcessCraftEntities = new ArrayList<>();
        abutmentProcessCraftEntities = queryAbutmentProcessCraft(abutmentOrderEntity.getTaskId());
        abutmentProcessCraftEntities.forEach(abutmentProcessCraftEntity -> {
            try {
                abutmentProcessCraftEntity.setInProcessTime((abutmentProcessCraftEntity.getStartTime().getTime() - abutmentProcessCraftEntity.getEndTime().getTime()) / (24 * 60 * 60 * 1000));
            }catch (NullPointerException e){
//                throw new NullPointerException("该工序缺少最早转入时间或最后转出时间");
            }
        });
        return abutmentProcessCraftEntities;
    }
    public List<AbutmentOrderEntity> abutmentOrder(AbutmentOrderReq abutmentOrderReq){
        LambdaQueryWrapper<AbutmentOrderEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (abutmentOrderReq.getStartTime()==null||abutmentOrderReq.getEndTime()==null){
            lambdaQueryWrapper.gt(AbutmentOrderEntity::getReleaseTime, LocalDateTime.of(LocalDate.now(), LocalTime.MIN));
        }else {
            if (abutmentOrderReq.getStartTime().equals(abutmentOrderReq.getEndTime())){
                Date date = abutmentOrderReq.getEndTime();
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(date);
                calendar.add(Calendar.DATE, 1);
                date = calendar.getTime();
                abutmentOrderReq.setEndTime(date);
            }
            lambdaQueryWrapper.between(AbutmentOrderEntity::getReleaseTime,abutmentOrderReq.getStartTime(),abutmentOrderReq.getEndTime());
        }
        if(CollectionUtil.isNotEmpty(abutmentOrderReq.getMaterialNos())){
            lambdaQueryWrapper.and(wq-> abutmentOrderReq.getMaterialNos().forEach(materialNo->wq.or().like(AbutmentOrderEntity::getMaterialNo, materialNo)));
        }
        lambdaQueryWrapper.orderByDesc(AbutmentOrderEntity::getReleaseTime);
        lambdaQueryWrapper.eq(AbutmentOrderEntity::getProductionBranchFactory,12);
        lambdaQueryWrapper.ne(AbutmentOrderEntity::getAbutmentStatus,9);
        List<AbutmentOrderEntity> abutmentOrderEntityList = abutmentOrderMapper.selectList(lambdaQueryWrapper);
        return abutmentOrderEntityList;
    }
    public List<AbutmentOrderEntity> abutmentOrder(List<String> taskIdList){
        LambdaQueryWrapper<AbutmentOrderEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (CollectionUtil.isNotEmpty(taskIdList))
        lambdaQueryWrapper.in(AbutmentOrderEntity::getTaskId,taskIdList);
        lambdaQueryWrapper.eq(AbutmentOrderEntity::getProductionBranchFactory,12);
        lambdaQueryWrapper.ne(AbutmentOrderEntity::getAbutmentStatus,9);
        List<AbutmentOrderEntity> abutmentOrderEntityList = abutmentOrderMapper.selectList(lambdaQueryWrapper);
        return abutmentOrderEntityList;
    }


    public List<AbutmentProcessCraftEntity> queryAbutmentProcessCraft(String taskId){
        List<AbutmentProcessCraftEntity> abutmentProcessCraftEntities = abutmentProcessCraftRepository.queryAbutmentProcessCraftData(taskId);
        if(CollectionUtil.isEmpty(abutmentProcessCraftEntities)){
            return new ArrayList<>();
        }
        abutmentProcessCraftEntities.forEach(abutmentProcessCraftEntity -> {
            if(ObjectUtil.isNull(abutmentProcessCraftEntity.getProductionType())){
                return;
            }
            try {
                abutmentProcessCraftEntity.setInProcessTime((abutmentProcessCraftEntity.getStartTime().getTime() - abutmentProcessCraftEntity.getEndTime().getTime()) / (24 * 60 * 60 * 1000));
            }catch (NullPointerException e){
//                throw new NullPointerException("该工序缺少最早转入时间或最后转出时间");
            }
            switch (abutmentProcessCraftEntity.getProductionType()){
                case 1:
                case 7:
                case 11:
                    abutmentProcessCraftEntity.setPlanTime(abutmentProcessCraftEntity.getProcesses04());
                    break;
                case 2:
                case 12:
                    abutmentProcessCraftEntity.setPlanTime(abutmentProcessCraftEntity.getProcesses10());
                    break;
                case 3:
                case 6:
                case 13:
                    abutmentProcessCraftEntity.setPlanTime(abutmentProcessCraftEntity.getProcesses07());
                    break;
                case 4:
                case 8:
                    abutmentProcessCraftEntity.setPlanTime(abutmentProcessCraftEntity.getProcesses05());
                    break;
                case 5:
                    abutmentProcessCraftEntity.setPlanTime(abutmentProcessCraftEntity.getProcesses09());
                    break;


            }
        });
        return abutmentProcessCraftEntities;

    }
    public List<AbutmentEntryExitRecordEntity> queryAbutmentEntryExitRecord(String startTime , String endTime){
//        String s = "2023-03-10";
        List<AbutmentEntryExitRecordEntity> abutmentProcessCraftEntities = abutmentEntryExitRecordRepository.queryAbutmentEntryExitRecord(startTime , endTime);
//        System.out.println(abutmentProcessCraftEntities.subList(0,10));
        return abutmentProcessCraftEntities;

    }

    public List<AbutmentOrderCraftEntity> queryAbutmentOrderCraft(List<String> taskIds){
        List<AbutmentOrderCraftEntity> abutmentOrderCraftEntities = new ArrayList<>();
        LambdaQueryWrapper<AbutmentOrderCraftEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AbutmentOrderCraftEntity::getTaskId,taskIds);
        queryWrapper.eq(AbutmentOrderCraftEntity::getProductionBranchFactory,12);
        List<AbutmentOrderCraftEntity> list = abutmentOrderCraftMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)){
            list.forEach(abutmentOrderCraftEntity -> {
                List<AbutmentProcessCraftEntity> abutmentProcessCraftEntityList = queryAbutmentProcessCraft(abutmentOrderCraftEntity.getTaskId());
                abutmentProcessCraftEntityList.forEach(abutmentProcessCraftEntity -> {
                    try {
                        abutmentProcessCraftEntity.setInProcessTime((abutmentProcessCraftEntity.getStartTime().getTime() - abutmentProcessCraftEntity.getEndTime().getTime()) / (24 * 60 * 60 * 1000));
                    }catch (NullPointerException e){
//                        throw new NullPointerException("该工序缺少最早转入时间或最后转出时间");
                    }
                });
                abutmentOrderCraftEntity.setAbutmentProcessCraftEntityList(abutmentProcessCraftEntityList);
                abutmentOrderCraftEntities.add(abutmentOrderCraftEntity);
            });
        }
        return abutmentOrderCraftEntities;
    }

    public List<String> queryProgram(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return abutmentSubProgramRepository.queryProgram(format.format(new Date()));
    }
}
